package server;

import net.sf.json.JSONObject;
import util.AddressHandle;
import util.Browser;
import util.Hash;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;

/**
 * User: tum0r
 * Package Name: server
 * Time: 2019-03-10 Sunday 19:35:28
 */
public class LoginServer extends BaseServer {
    public boolean checkLogin(String username, String cookie) {
        return sql.query("select login.* from login,user where user.username=? and login.user_id=user.uuid and login.cookie=?", new Object[]{username, cookie}).size() == 0;
    }

    public boolean updateLoginTime(String username) {
        return sql.update("update login set time=? where user_name=?", new Object[]{new Date().getTime(), username}) == 1;
    }

    public void deleteLogin(String username) {
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select uuid from user where username=?", new Object[]{username});
        if (temp.size() > 0) {
            String uuid = temp.get(0).get("uuid").toString();
            sql.update("delete from login where user_id=?", new Object[]{uuid});
        }
    }

    public int getUserType(String username) {
        int result = -1;
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select user_type from login where user_name=?", new Object[]{username});
        if (temp.size() > 0) {
            result = Integer.parseInt(temp.get(0).get("user_type").toString());
        }
        return result;
    }

    public JSONObject login(String username, String password, String cookie, String verificationCode) {
        JSONObject result = JSONObject.fromObject("{}");
        result.put("state", "false");
        result.put("content", "登录失败，原因未知");
        result.put("userID", "");
        VerificationCodeServer verificationCodeServer = new VerificationCodeServer();
        verificationCodeServer.open();
        if (verificationCodeServer.checkVerificationCode(cookie, verificationCode)) {
            Hash hash = new Hash();
            LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select * from user where username=? and password=? and state=?", new Object[]{username, hash.passwordEncryption(password), 1});
            if (temp.size() != 0) {
                result.replace("state", "true");
                result.replace("content", "登录成功");
                result.replace("userID", temp.get(0).get("uuid").toString());
            } else {
                result.replace("content", "登录失败，用户名或密码错误，或此账号已被封禁");
            }
        } else {
            result.replace("content", "登录失败，验证码错误");
        }
        verificationCodeServer.close();
        return result;
    }

    public void record(String username, HttpServletRequest request) {
        record(username, request.getSession().getAttribute("cookie").toString(), request.getRemoteAddr(), request.getHeader("user-agent"));
    }

    public void record(String username, String cookie, String host, String userAgent) {
        LinkedList<LinkedHashMap<String, Object>> temp = sql.query("select uuid,type from user where username=?", new Object[]{username});
        if (temp.size() > 0) {
            String uuid = temp.get(0).get("uuid").toString();
            String type = temp.get(0).get("type").toString();

            Browser browser = new Browser();
            JSONObject browserResult = browser.getBrowser(userAgent);

            AddressHandle addressHandle = new AddressHandle();
            sql.update("update login set user_id=?,user_name=?,user_type=?,host=?,browser=?,time=? where cookie=?", new Object[]{uuid, username, type, addressHandle.handle(host), browserResult.toString(), new Date().getTime(), cookie});
        }
    }

}
